package com.example.carrierapp.ui.main.dispatch.add

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import com.example.carrierapp.base.BaseActivity
import com.example.carrierapp.base.MVPBaseActivity
import com.example.carrierapp.config.hide
import com.example.carrierapp.config.toast
import com.example.carrierapp.ui.guidedialog.GuideDialog
import com.example.carrierapp.ui.main.dispatch.search.DispatchSearchActivity
import com.example.carrierapp.util.ActivityUtil
import com.example.carrierapp.util.LogUtil
import com.example.carrierapp.util.ToastUtils
import com.example.carrierapp.viewmodel.itemmodel.ItemSearchModel
import com.example.lb.fingerdemo.R
import com.google.gson.Gson
import kotlinx.android.synthetic.main.activity_dispatch_add_new.*

class DispatchAddActivity : MVPBaseActivity<DispatchAddContract.View, DispatchAddPresenter>(), DispatchAddContract.View {
    override fun createPresenter() = DispatchAddPresenter(this)
    private val isSingle by lazy { intent.getBooleanExtra("isSingle", false) }
    private val shipmentId by lazy { intent.getIntExtra("shipmentId", 0) }
    private var dividerInfo: ItemSearchModel? = null
    private var carInfo: ItemSearchModel? = null

    private val guideDialog by lazy { GuideDialog(this) }

    override fun getTitleString() = intent.getStringExtra("shipmentNumber") ?: "承运宝"

    override fun getContentView() = R.layout.activity_dispatch_add_new

    override fun showBottomLine() = false

    override fun initView() {
        if (isSingle.not())
            notice_view.hide()
        input_num.hint = "0<= 装车量 <=120"
        guideDialog.show(3)
    }

    override fun initEvent() {
        confirm.setOnClickListener {
            when {
                carInfo == null -> toast("请完善车辆信息")
                dividerInfo == null -> toast("请完善驾驶员信息")
                driver_phone.text.isEmpty() -> toast("请输入驾驶员电话")
                input_num.text.isEmpty() -> toast("请输入装车量")
                input_num.text.toString().toFloat() < 0f || input_num.text.toString().toFloat() > 120f -> toast("请输入正确的装车量")
                else -> {
                    dividerInfo?.phone = driver_phone.text.toString()
                    mPresenter.addDispatch(carInfo, dividerInfo, input_num.text.toString(), remark.text.toString(), shipmentId)
                }
            }
        }
        search_car.setOnClickListener {
            ActivityUtil.startDispatchSearchForResult(this, DispatchSearchActivity.SEARCH_TYPE_CARNUM, DispatchSearchActivity.SEARCH_TYPE_CARNUM)
        }

        search_driver.setOnClickListener {
            ActivityUtil.startDispatchSearchForResult(this, DispatchSearchActivity.SEARCH_TYPE_DRIVER, DispatchSearchActivity.SEARCH_TYPE_DRIVER)
        }

    }

    override fun addDispatchSuccess(bean: String) {
        ToastUtils.showCenterToast(this, "调度成功")
        finish()
    }

    override fun addDispatchFailde(msg: String) {
        ToastUtils.showCenterToast(this, msg)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if (resultCode == Activity.RESULT_OK) {
            data?.let {
                when (requestCode) {
                    DispatchSearchActivity.SEARCH_TYPE_CARNUM -> {
                        carInfo = Gson().fromJson(it.getStringExtra("info"), ItemSearchModel::class.java)
                        search_car.text = carInfo?.name
                    }
                    DispatchSearchActivity.SEARCH_TYPE_DRIVER -> {
                        dividerInfo = Gson().fromJson(it.getStringExtra("info"), ItemSearchModel::class.java)
                        search_driver.text = dividerInfo?.name
                        driver_phone.setText(dividerInfo?.phone)
                    }
                }
            }
        }
        super.onActivityResult(requestCode, resultCode, data)
    }
}
